package org.cable.shop.component;

import lombok.extern.slf4j.Slf4j;
import org.cable.common.enums.ProductOrderPayTypeEnum;
import org.cable.shop.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author hsj
 * @date 2023/11/22 14:13
 */
@Component
@Slf4j
public class PayFactory {

	@Autowired
	private AliPayStrategy aliPayStrategy;

	@Autowired
	private WechatPayStrategy wechatPayStrategy;

	/**
	 * 创建支付,简单工厂模式
	 * @param payInfoVO
	 * @return
	 */
	public String pay(PayInfoVO payInfoVO){
		String payType = payInfoVO.getPayType();

		if (ProductOrderPayTypeEnum.ALI_PAY.name().equals(payType)){
			PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
			return payStrategyContext.executeUnifiedOrder(payInfoVO);


		} else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equals(payType)) {
			PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
			return payStrategyContext.executeUnifiedOrder(payInfoVO);
		}
		return "";
	}



	/**
	 * 关闭订单支付,简单工厂模式
	 * @param payInfoVO
	 * @return
	 */
	public String closeOrder(PayInfoVO payInfoVO){
		String payType = payInfoVO.getPayType();

		if (ProductOrderPayTypeEnum.ALI_PAY.name().equals(payType)){
			PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
			return payStrategyContext.executeCloseOrder(payInfoVO);
		} else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equals(payType)) {
			PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
			return payStrategyContext.executeCloseOrder(payInfoVO);
		}
		return "";
	}


	/**
	 * 关闭订单支付,简单工厂模式
	 * @param payInfoVO
	 * @return
	 */
	public String queryPayStatus(PayInfoVO payInfoVO){
		String payType = payInfoVO.getPayType();

		if (ProductOrderPayTypeEnum.ALI_PAY.name().equals(payType)){
			PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
			return payStrategyContext.executeQueryStatus(payInfoVO);
		} else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equals(payType)) {
			PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
			return payStrategyContext.executeQueryStatus(payInfoVO);
		}
		return "";
	}


	/**
	 * 退款接口
	 * @param payInfoVO
	 * @return
	 */
	public String refundOrder(PayInfoVO payInfoVO){
		String payType = payInfoVO.getPayType();

		if (ProductOrderPayTypeEnum.ALI_PAY.name().equals(payType)){
			PayStrategyContext payStrategyContext = new PayStrategyContext(aliPayStrategy);
			return payStrategyContext.executeRefund(payInfoVO);
		} else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equals(payType)) {
			PayStrategyContext payStrategyContext = new PayStrategyContext(wechatPayStrategy);
			return payStrategyContext.executeRefund(payInfoVO);
		}
		return "";
	}
}
